﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Resources;

namespace DiamondDash.Core
{
    public class SoundManager
    {
        public const int eliminate1 = 5;
        public const int eliminate2 = 6;
        public const int eliminate3 = 7;
        public const int eliminate4 = 8;
        public const int eliminate5 = 9;
        public const int eliminate6 = 10;
        public const int eliminate7 = 11;
        public const int eliminate8 = 12;
        public const int eliminate9 = 13;
        public const int eliminate10 = 14;
        public const int eliminate11 = 4;
        public const int eliminate12 = 3;
        public const int highScore = 15;
        public const int bombEliminate = 16;
        public const int clickButton = 17;
        public const int addTime = 18;
        public const int fireModeEnv = 19;
        public const int newSuperDiamond = 20;
        public const int colorEliminate = 21;
        public const int readyGo = 22;
        public const int levelup = 23;

        static SoundEffectInstance fireSoundIs = null;

        private static Dictionary<int, SoundEffect> effects = new Dictionary<int, SoundEffect>();

        public static void PlayFireEnv()
        {
            if (!StageManager.Instance.IsPlaySound)
            {
                return;
            }
            fireSoundIs.Play();
        }

        public static void StopFireEnv()
        {
            if (!StageManager.Instance.IsPlaySound)
            {
                return;
            }
            fireSoundIs.Stop();
        }

        /// <summary>
        /// 播放音效
        /// </summary>
        /// <param name="id"></param>
        public static void PlayEffect(int id)
        {
            if (!StageManager.Instance.IsPlaySound)
            {
                return;
            }
            if (effects.ContainsKey(id))
            {
                FrameworkDispatcher.Update();
                effects[id].Play();
            }
        }

        /// <summary>
        /// 停止音效
        /// </summary>
        /// <param name="id"></param>
        public static void StopEffect(int id)
        {
            if (!StageManager.Instance.IsPlaySound)
            {
                return;
            }
            if (effects.ContainsKey(id))
            {
                FrameworkDispatcher.Update();
                effects[id].CreateInstance().Stop();
            }
        }

        /// <summary>
        /// 预先加载声音文件
        /// </summary>
        /// <param name="SoundFilePath"></param>
        /// <returns></returns>
        public static SoundEffect PreLoadSound(int id, String SoundFilePath)
        {
            SoundEffect Sound = null;
            try
            {
                StreamResourceInfo SoundFileInfo = Application.GetResourceStream(new Uri(SoundFilePath, UriKind.Relative));
                Sound = SoundEffect.FromStream(SoundFileInfo.Stream);
                effects[id] = Sound;
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("Couldn't load sound!" + SoundFilePath);
            }

            if(id == fireModeEnv)
            {
                fireSoundIs = Sound.CreateInstance();
            }

            return Sound;
        }
    }
}
